{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "e:\\Program\\MyTest\\Python\\demo\n",
      "e:\\Program\\MyTest\\Python\n",
      "False\n",
      "False\n",
      "False\n",
      "/Users/wenwensong/我的仓库/MyTest/Python/test/a\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "\n",
    "# . 当前所在的绝对路径\n",
    "# .. 当前的上一级所在的绝对路径\n",
    "print(os.path.abspath('.'))\n",
    "print(os.path.abspath('..'))\n",
    "\n",
    "# 返回路径的文件夹部分\n",
    "os.path.dirname('/Users/wenwensong/我的仓库/MyTest/Python/demo/正则库.ipynb')\n",
    "\n",
    "# 判断目录是否存在\n",
    "print(os.path.exists('/Users'))\n",
    "\n",
    "# 判断是否是目录\n",
    "print(os.path.isdir('/Users'))\n",
    "\n",
    "# 判断是否是文件\n",
    "print(os.path.isfile(u'/Users/wenwensong/我的仓库/MyTest/Python/demo/正则库.ipynb'))\n",
    "\n",
    "# 路径拼接\n",
    "print(os.path.join('/Users/wenwensong/我的仓库/MyTest/Python/','test/a'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pathlib import Path\n",
    "\n",
    "# 获取当前路径，先将 . 格式化成 Path 对象\n",
    "p = Path('.')\n",
    "p.resolve()\n",
    "\n",
    "p.is_dir()\n",
    "p.is_file()\n",
    "\n",
    "# 新建目录\n",
    "new_dir = Path('../haha')\n",
    "Path.mkdir(new_dir,parents=False)  # parents 父级目录不存在时是否一并新建"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['第100节  放下控制.mp4']\n"
     ]
    }
   ],
   "source": [
    "# 文件批量重命名\n",
    "import os\n",
    "\n",
    "# 存放文件的路径\n",
    "file_path = r'C:\\\\Users\\\\13675\\\\Desktop'\n",
    "# 要修改文件的后缀\n",
    "old_ext = '.mp4'\n",
    "# 新的后缀\n",
    "new_ext = '.mp3'\n",
    "# 获取指定文件夹下的文件列表\n",
    "file_list = os.listdir(file_path)\n",
    "\n",
    "for name in file_list:\n",
    "    if name.endswith(old_ext):\n",
    "        # 拼接完整的文件路径，原名称的\n",
    "        old_path = os.path.join(file_path, name)\n",
    "\n",
    "        # 拼接完整的文件路径，新名称的\n",
    "        new_path = os.path.join(file_path, name[:-4] + new_ext)\n",
    "\n",
    "        # 重命名\n",
    "        os.rename(old_path, new_path)\n",
    "\n",
    "result = [ r for r in os.listdir(file_path) if r.endswith(new_ext)]\n",
    "\n",
    "print(result)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['第100节  放下控制.mp3']"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import os\n",
    "\n",
    "def rename(path, old_ext, new_ext):\n",
    "    \"\"\"\n",
    "    :param path: 文件所在路径，不需要最后的 \\\n",
    "    :param old_ext: 要修改的文件后缀，例如 .mp3\n",
    "    :param new_ext: 修改为新的后缀，例如 .mp4\n",
    "    :return: 返回已修改的文件名称列表\n",
    "    \"\"\"\n",
    "    # 获取指定文件夹下的文件列表\n",
    "    file_list = os.listdir(path)\n",
    "    for name in file_list:\n",
    "        # 过滤出给定后缀的文件\n",
    "        if name.endswith(old_ext):\n",
    "            # 拼接完整的文件路径，原名称的\n",
    "            old_path = os.path.join(path, name)\n",
    "\n",
    "            # 拼接完整的文件路径，新名称的\n",
    "            new_path = os.path.join(path, name[:-4] + new_ext)\n",
    "\n",
    "            # 重命名\n",
    "            os.rename(old_path, new_path)\n",
    "\n",
    "    # 返回被修改的文件列表\n",
    "    result = [ r for r in os.listdir(path) if r.endswith(new_ext)]\n",
    "    return result\n",
    "    \n",
    "rename(r'C:\\\\Users\\\\13675\\\\Desktop', '.mp4', '.mp3')"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "ff79ebfcf6806606a412477be2872c3ec52671763ecb054ca3010c66083164a3"
  },
  "kernelspec": {
   "display_name": "Python 3.8.3 ('base')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
